#include "tingssearhingwidget.h"

TingsSearhingWidget::TingsSearhingWidget(QWidget *parent) : QWidget(parent)
{
    init_Widget();
    init_Control();
    init_Connect();
}

void TingsSearhingWidget::init_Control()
{
    this->table=new QTableWidget(this);
    this->index=0;

    this->table->setColumnCount(5);
    this->table->resize(800,500);
    this->table->setHorizontalHeaderItem(0,new QTableWidgetItem("物品id"));
    this->table->setHorizontalHeaderItem(1,new QTableWidgetItem("物品名"));
    this->table->setHorizontalHeaderItem(2,new QTableWidgetItem("物品类型"));
    this->table->setHorizontalHeaderItem(3,new QTableWidgetItem("物品价格"));
    this->table->setHorizontalHeaderItem(4,new QTableWidgetItem("物品数量"));

    this->table->setEditTriggers(QAbstractItemView::NoEditTriggers);

    this->confirmEt=new QLineEdit();
    this->confirmEt->setPlaceholderText("请输入查找根据");

    this->returnBtn=new QPushButton();
    this->returnBtn->setText(QString::fromUtf8("返回"));
    this->confirmBtn=new QPushButton();
    this->confirmBtn->setText(QString::fromUtf8("确认"));


    this->confirmCombo=new QComboBox();
    this->confirmCombo->addItem("物品id");
    this->confirmCombo->addItem("物品名");
    this->confirmCombo->addItem("物品类型");

    this->btnhLay=new QHBoxLayout();
    this->btnhLay->addWidget(this->confirmBtn);
    this->btnhLay->addWidget(this->returnBtn);

    this->boxhLay=new QHBoxLayout();
    this->boxhLay->addWidget(this->confirmCombo);
    this->boxhLay->addWidget(this->confirmEt);



    this->vLay=new QVBoxLayout();
    this->vLay->addLayout(this->boxhLay);
    this->vLay->addLayout(this->btnhLay);
    this->vLay->addWidget(this->table);
    this->vLay->setContentsMargins(100,50,100,50);

    this->setLayout(this->vLay);
}

void TingsSearhingWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("搜索物品"));
    this->resize(800,600);
}

void TingsSearhingWidget::init_Connect()
{
    connect(this->returnBtn,SIGNAL(clicked()),this,SLOT(on_retutrnBtn_cliked()));
    connect(this->confirmBtn,SIGNAL(clicked()),this,SLOT(on_confirmBtn_cliked()));
    connect(confirmCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_Index_Changed(int)));
}


int TingsSearhingWidget::userAlter()
{
    //实现修改操作
    QString Info,id,sql;
    int stocknumber=0;
    this->addstock.resize(100);
    Info=this->confirmEt->text();
    if(this->index==0)
    {
        sql=QString("SELECT *FROM StockRecord WHERE tings_id=%1;").arg(Info);
    }
    else if (this->index==1)
    {
        sql=QString("SELECT *FROM StockRecord WHERE tings_name='%1';").arg(Info);
    }
    else if (this->index==2)
    {
        sql=QString("SELECT *FROM StockRecord WHERE tings_jur='%1';").arg(Info);
    }


        this->model=this->control->getInstance()->Creatmodel("wuping");
        addstock=this->model->getStock((char*)sql.toStdString().c_str());

        if(addstock[0].getUser_id()!=0)
        {


            //找到stock容器内的有效值个数
            for(int i=0;i<this->addstock.size();i++)
            {
                if(addstock[i].getUser_id()!=0)
                    stocknumber++;
            }

             qDebug()<<"stocknumber:"<<stocknumber;
             table->setRowCount(stocknumber);
            //进行表格填充
            this->confirmEt->clear();
            for(int i=0;i<stocknumber;i++)
           {
               int j=0;
                qDebug()<<"运行插入表数据"<<addstock.size();
               QString user_identity="普通用户";
                //将int型转换成string型
                QString tingsIdStr = QString::number(this->addstock[i].getTings_id());
                QString tingsMoney = QString::number(this->addstock[i].getTings_money());
                QString tingscount = QString::number(this->addstock[i].getTings_count());

                //将所有用户信息放入表中
                this->table->setItem(i,j++,new QTableWidgetItem(tingsIdStr));
                this->table->setItem(i,j++,new QTableWidgetItem(this->addstock[i].getTings_name()));
                this->table->setItem(i,j++,new QTableWidgetItem(this->addstock[i].getTings_jur()));
                this->table->setItem(i,j++,new QTableWidgetItem(tingsMoney));
                this->table->setItem(i,j++,new QTableWidgetItem(tingscount));
                this->table->viewport()->update();


            }
        }
        else
        {
            QMessageBox::critical(this, "错误", "修改失败");
            this->confirmEt->clear();

            return 0;
        }



    return 1;
}

void TingsSearhingWidget::on_retutrnBtn_cliked()
{

    emit returnWidget();
}

void TingsSearhingWidget::on_confirmBtn_cliked()
{
    userAlter();

}

void TingsSearhingWidget::on_Index_Changed(int index)
{
    //查看修改什么
    this->index=index;
}
